package com.cares.entity.base;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;

public abstract class IEntity implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6369940047627435431L;
	protected String props;// 属性,专门用来数据库扩展
	protected LinkedHashMap<String, String> propMap;
	


	public static final String DELIMITER = ";;";

	public static final String ASSIGNATION = ":=";
	public String getProps() {
		return props;
	}

	public void setProps(String props) {
		this.props = props;
	}
	public Map<String, String> getPropMap() {
		if (propMap == null) {
			propMap = new LinkedHashMap<String, String>();
			String original = getProps();
			if (original != null && original.length() > 0) {
				int separatorLength = ASSIGNATION.length();
				String[] tokens = original.split(DELIMITER);
				for (String token : tokens) {
					int index = token.indexOf(ASSIGNATION);
					if (index != -1) {
						String key = token.substring(0, index);
						String value = token.substring(index + separatorLength);
						propMap.put(key, value);
					} else {
						throw new IllegalArgumentException("Parse ERROR");
					}
				}
			}
		}
		return propMap;
	}

	public String getProp(String key) {
		Map<String, String> map = getPropMap();
		return map.get(key);
	}

	public void setProp(String key, String value) {
		if (key != null) {
			Map<String, String> map = getPropMap();
			map.put(key, value);
			StringBuffer buf = new StringBuffer();
			for (Map.Entry<String, String> entry : map.entrySet()) {
				if (entry.getKey() != null) {
					buf.append(entry.getKey());
					buf.append(ASSIGNATION);
					buf.append(entry.getValue());
					buf.append(DELIMITER);
				}
			}
			setProps(buf.toString());
		}
	}

}
